//
//  DrawView.m
//  Blue-toothLight
//
//  Created by mazg on 15/6/23.
//  Copyright (c) 2015年 mazg. All rights reserved.
//

#import "DrawView.h"

@implementation DrawView

- (void)drawRect:(CGRect)rect {
    
//暂时注释掉
    /*
    float width=rect.size.width;
    float height=rect.size.height;
    
    // 1.获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 2.拼接路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    CGPoint startP = CGPointMake(0, 5);
    [path moveToPoint:startP];
    [path addQuadCurveToPoint:CGPointMake(width, 5) controlPoint:CGPointMake(width/2.0f, -5)];
    [path addLineToPoint:CGPointMake(width-3, height)];
    [path addQuadCurveToPoint:CGPointMake(3, height) controlPoint:CGPointMake(width/2, 15)];
    [path addLineToPoint:startP];
    CGContextAddPath(ctx, path.CGPath);
    [self.color setFill];
    CGContextDrawPath(ctx, kCGPathFill);
    */

    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    if (self.colorArr.count==0) {
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
        CGContextAddPath(ctx, path.CGPath);
        [[UIColor colorWithRed:216/255.0f green:208/255.0f blue:209/255.0f alpha:1.0f] setFill];
        CGContextDrawPath(ctx, kCGPathFill);
    }
    else if (self.colorArr.count==1) {
        UIColor *color1=self.colorArr[0];
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
        CGContextAddPath(ctx, path.CGPath);
        [color1 setFill];
        CGContextDrawPath(ctx, kCGPathFill);
    }
    else{
        CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
        CGFloat r,g,b,a;
        UIColor *color1=self.colorArr[0];
        [color1 getRed:&r green:&g blue:&b alpha:&a];
        
        UIColor *color2=self.colorArr[1];
        CGFloat r1,g1,b1,a1;
        [color2 getRed:&r1 green:&g1 blue:&b1 alpha:&a1];
        
        // 创建起点颜色
        CGColorRef beginColor = CGColorCreate(colorSpaceRef, (CGFloat[]){r, g, b, a});
        // 创建终点颜色
        CGColorRef endColor = CGColorCreate(colorSpaceRef, (CGFloat[]){r1, g1, b1, a1});
        // 创建颜色数组
        CFArrayRef colorArray = CFArrayCreate(kCFAllocatorDefault, (const void*[]){beginColor, endColor}, 2, nil);
        // 创建渐变对象
        CGGradientRef gradientRef = CGGradientCreateWithColors(colorSpaceRef, colorArray, (CGFloat[]){
            0.0f,	   // 对应起点颜色位置
            1.0f		// 对应终点颜色位置
        });
        // 释放颜色数组
        CFRelease(colorArray);
        // 释放起点和终点颜色
        CGColorRelease(beginColor);
        CGColorRelease(endColor);
        // 释放色彩空间
        CGColorSpaceRelease(colorSpaceRef);
        CGContextDrawLinearGradient(ctx, gradientRef, CGPointMake(0, 0), CGPointMake(rect.size.width, rect.size.height), 0);
        // 释放渐变对象
        CGGradientRelease(gradientRef);
        
    }
}

@end
